package com.rzico.market.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.plugin.jd.JDServiceUtils;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "京东售后与财务模块接口")
@RestController
@RequestMapping("/admin/jdPlugin")
public class JDPluginController extends BaseController {

    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 查询企业的账号余额
     *
     * @return
     */
    @ApiOperation("查询企业的账号余额")
    @GetMapping("/getJDBalance/{pin}")
    public CommResult<Map<String, Object>> getJDBalance(@PathVariable String pin) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        //获取京东授权token
        String token = JDServiceUtils.refrash_token();
        String type = "1";//类型，表示查询企业充值的账号余额
        Map<String, Object> resultMap = JDServiceUtils.getBalance(token, pin, type);
        Object remainLimit = resultMap.get("remainLimit");
        return CommResult.success(remainLimit);
    }

    /**
     * 查询京东余额变动明细
     *
     * @return
     */
    @ApiOperation("查询京东余额变动明细")
    @GetMapping("/getJDBalanceDetail")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间(yyyyMMdd)", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间(yyyyMMdd)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderId", value = "订单号", dataType = "Integer", paramType = "query")
    })
    public CommResult<Map<String, Object>> getJDBalanceDetail(String startDate, String endDate, Pageable pageable, String orderId) {
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        //获取京东授权token
        String token = JDServiceUtils.refrash_token();
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        Map<String, Object> resultMap = JDServiceUtils.getBalanceDetail(token, pageable.getPageNum(), pageable.getPageSize(), orderId, startDate, endDate);
        List<Map> list = JSONArray.parseArray(resultMap.get("data").toString(), Map.class);
        PageResult<Map> pageResult = new PageResult<Map>(list, Long.valueOf(resultMap.get("total").toString()), pageable);
        return CommResult.success(pageResult);
    }


}
